/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.tnmk.worktime.gui.history.filter;

import org.tnmk.common.util.Filter;
import org.tnmk.common.util.WildcardUtils;

/**
 * Wildcards == Glob
 *
 * @author Administrator
 */
public class GlobFilter implements Filter<String> {

  private String regex;
  public GlobFilter(String wildcard) {
    this.regex = toRegex(wildcard);
  }

  protected String toRegex(String wildcard) {
    if (wildcard != null && !wildcard.isEmpty()) {
      return WildcardUtils.wildcardToRegex(wildcard.toLowerCase());
    }
    return null;
  }

  @Override
  public boolean accept(String s) {
    if (regex == null) {
      return true;//no filter
    }
    if (s == null) {
      return false;
    }
    return s.toLowerCase().matches(regex);
  }

  protected void setRegex(String regex) {
    this.regex = regex;
  }

  public String getRegex() {
    return regex;
  }

}
